package com.dianping.cat.consumer.storage.model.transform;

import com.dianping.cat.consumer.storage.model.IEntity;
import com.dianping.cat.consumer.storage.model.IVisitor;
import com.dianping.cat.consumer.storage.model.entity.Domain;
import com.dianping.cat.consumer.storage.model.entity.Machine;
import com.dianping.cat.consumer.storage.model.entity.Operation;
import com.dianping.cat.consumer.storage.model.entity.Segment;
import com.dianping.cat.consumer.storage.model.entity.Sql;
import com.dianping.cat.consumer.storage.model.entity.StorageReport;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;






















public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  47 */   public DefaultXmlBuilder() { this(false); }



/*  51 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  55 */     this.m_compact = compact;
/*  56 */     this.m_sb = sb;
/*  57 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  61 */     entity.accept(this.m_visitor);
/*  62 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  66 */     this.m_level--;

/*  68 */     indent();
/*  69 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  73 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  77 */     if (value == null) {
      return null;
    }

/*  81 */     String str = toString(value);
/*  82 */     int len = str.length();
/*  83 */     StringBuilder sb = new StringBuilder(len + 16);

/*  85 */     for (int i = 0; i < len; i++) {
/*  86 */       char ch = str.charAt(i);

/*  88 */       switch (ch) {
        case '<':
/*  90 */           sb.append("&lt;");
          break;
        case '>':
/*  93 */           sb.append("&gt;");
          break;
        case '&':
/*  96 */           sb.append("&amp;");
          break;
        case '"':
/*  99 */           if (!text) {
/* 100 */             sb.append("&quot;");
            break;
          }
        default:
/* 104 */           sb.append(ch);
          break;
      }

    }
/* 109 */     return sb.toString();
  }

  protected void indent() {
/* 113 */     if (!this.m_compact) {
/* 114 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 115 */         this.m_sb.append("   ");
      }
    }
  }


/* 121 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 125 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 129 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 133 */     indent();

/* 135 */     this.m_sb.append('<').append(name);

/* 137 */     int len = nameValues.length;

/* 139 */     for (int i = 0; i + 1 < len; i += 2) {
/* 140 */       Object attrName = nameValues[i];
/* 141 */       Object attrValue = nameValues[i + 1];

/* 143 */       if (attrValue != null) {
/* 144 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 148 */     if (dynamicAttributes != null) {
/* 149 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 150 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 154 */     if (text != null && closed) {
/* 155 */       this.m_sb.append('>');
/* 156 */       this.m_sb.append(escape(text, true));
/* 157 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 159 */       if (closed) {
/* 160 */         this.m_sb.append('/');
      } else {
/* 162 */         this.m_level++;
      }

/* 165 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 171 */     if (value instanceof String)
/* 172 */       return (String)value; 
/* 173 */     if (value instanceof Collection) {
/* 174 */       Collection<Object> list = (Collection)value;
/* 175 */       StringBuilder sb = new StringBuilder(32);
/* 176 */       boolean first = true;

/* 178 */       for (Object item : list) {
/* 179 */         if (first) {
/* 180 */           first = false;
        } else {
/* 182 */           sb.append(',');
        }

/* 185 */         if (item != null) {
/* 186 */           sb.append(item);
        }
      }

/* 190 */       return sb.toString();
/* 191 */     }  if (value.getClass().isArray()) {
/* 192 */       int len = Array.getLength(value);
/* 193 */       StringBuilder sb = new StringBuilder(32);
/* 194 */       boolean first = true;

/* 196 */       for (int i = 0; i < len; i++) {
/* 197 */         Object item = Array.get(value, i);

/* 199 */         if (first) {
/* 200 */           first = false;
        } else {
/* 202 */           sb.append(',');
        }

/* 205 */         if (item != null) {
/* 206 */           sb.append(item);
        }
      }

/* 210 */       return sb.toString();
    }

/* 213 */     return String.valueOf(value);
  }

  protected void tagWithText(String name, String text, Object... nameValues) {
/* 217 */     if (text == null) {
      return;
    }

/* 221 */     indent();

/* 223 */     this.m_sb.append('<').append(name);

/* 225 */     int len = nameValues.length;

/* 227 */     for (int i = 0; i + 1 < len; i += 2) {
/* 228 */       Object attrName = nameValues[i];
/* 229 */       Object attrValue = nameValues[i + 1];

/* 231 */       if (attrValue != null) {
/* 232 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 236 */     this.m_sb.append(">");
/* 237 */     this.m_sb.append(escape(text, true));
/* 238 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  protected void element(String name, String text, String defaultValue, boolean escape) {
/* 242 */     if (text == null || text.equals(defaultValue)) {
      return;
    }

/* 246 */     indent();

/* 248 */     this.m_sb.append('<').append(name).append(">");

/* 250 */     if (escape) {
/* 251 */       this.m_sb.append(escape(text, true));
    } else {
/* 253 */       this.m_sb.append("<![CDATA[").append(text).append("]]>");
    }

/* 256 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  protected String toString(Date date, String format) {
/* 260 */     if (date != null) {
/* 261 */       return (new SimpleDateFormat(format)).format(date);
    }
/* 263 */     return null;
  }


  protected String toString(Number number, String format) {
/* 268 */     if (number != null) {
/* 269 */       return (new DecimalFormat(format)).format(number);
    }
/* 271 */     return null;
  }



  public void visitDomain(Domain domain) {
/* 277 */     startTag("domain", null, new Object[] { "id", domain.getId() });

/* 279 */     if (!domain.getOperations().isEmpty()) {
/* 280 */       for (Operation operation : domain.getOperations().values()) {
/* 281 */         operation.accept(this.m_visitor);
      }
    }

/* 285 */     if (!domain.getSqls().isEmpty()) {
/* 286 */       for (Sql sql : domain.getSqls().values()) {
/* 287 */         sql.accept(this.m_visitor);
      }
    }

/* 291 */     endTag("domain");
  }


  public void visitMachine(Machine machine) {
/* 296 */     startTag("machine", null, new Object[] { "id", machine.getId() });

/* 298 */     if (!machine.getDomains().isEmpty()) {
/* 299 */       for (Domain domain : machine.getDomains().values()) {
/* 300 */         domain.accept(this.m_visitor);
      }
    }

/* 304 */     endTag("machine");
  }


  public void visitOperation(Operation operation) {
/* 309 */     startTag("operation", null, new Object[] { "id", operation.getId(), "count", Long.valueOf(operation.getCount()), "avg", toString(Double.valueOf(operation.getAvg()), "0.0"), "sum", toString(Double.valueOf(operation.getSum()), "0.0"), "error", Long.valueOf(operation.getError()), "long-count", Long.valueOf(operation.getLongCount()) });

/* 311 */     if (!operation.getSegments().isEmpty()) {
/* 312 */       for (Segment segment : operation.getSegments().values()) {
/* 313 */         segment.accept(this.m_visitor);
      }
    }

/* 317 */     endTag("operation");
  }



/* 322 */   public void visitSegment(Segment segment) { startTag("segment", true, null, new Object[] { "id", Integer.valueOf(segment.getId()), "count", Long.valueOf(segment.getCount()), "avg", toString(Double.valueOf(segment.getAvg()), "0.0"), "sum", toString(Double.valueOf(segment.getSum()), "0.0"), "error", Long.valueOf(segment.getError()), "long-count", Long.valueOf(segment.getLongCount()) }); }




/* 327 */   public void visitSql(Sql sql) { startTag("sql", true, null, new Object[] { "id", sql.getId(), "statement", sql.getStatement(), "count", Integer.valueOf(sql.getCount()) }); }



  public void visitStorageReport(StorageReport storageReport) {
/* 332 */     startTag("storage-report", null, new Object[] { "id", storageReport.getId(), "name", storageReport.getName(), "type", storageReport.getType(), "startTime", toString(storageReport.getStartTime(), "yyyy-MM-dd HH:mm:ss"), "endTime", toString(storageReport.getEndTime(), "yyyy-MM-dd HH:mm:ss") });

/* 334 */     if (!storageReport.getIds().isEmpty()) {
/* 335 */       for (String id : storageReport.getIds()) {
/* 336 */         tagWithText("id", id, new Object[0]);
      }
    }

/* 340 */     if (!storageReport.getIps().isEmpty()) {
/* 341 */       for (String ip : storageReport.getIps()) {
/* 342 */         tagWithText("ip", ip, new Object[0]);
      }
    }

/* 346 */     if (!storageReport.getOps().isEmpty()) {
/* 347 */       for (String op : storageReport.getOps()) {
/* 348 */         tagWithText("op", op, new Object[0]);
      }
    }

/* 352 */     if (!storageReport.getMachines().isEmpty()) {
/* 353 */       for (Machine machine : storageReport.getMachines().values()) {
/* 354 */         machine.accept(this.m_visitor);
      }
    }

/* 358 */     endTag("storage-report");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/storage/model/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */